
/*
 *   Copyright [Xin Li] schindlerlee@gmail.com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <pthread.h>
#include "TM.h"

using namespace std;
using namespace TM;

Mutex mut;
class Task1 :public Job{
public:
	void run(void)
	{
		mut.lock();
		for (int i = 0;i < 100;i++) {
			usleep(1);
			printf("1");
			fflush(stdout);
		}
		putchar(10); fflush(stdout);
		mut.unlock();
	}
};

class Task2: public Task1
{
public: 
	void run()
	{
		mut.lock();
		for (int i = 0;i < 100;i++) {
			usleep(1);
			printf("2");
			fflush(stdout);
		}
		putchar(10); fflush(stdout);
		mut.unlock();
	}
};


class Task3 :public Job{
public:
	void run(void)
	{
		for (int i = 0;i < 100;i++) {
			usleep(1);
			printf("1");
			fflush(stdout);
		}
		putchar(10); fflush(stdout);
	}
};

class Task4: public Task1
{
public: 
	void run()
	{
		for (int i = 0;i < 100;i++) {
			usleep(1);
			printf("2");
			fflush(stdout);
		}
		putchar(10); fflush(stdout);
	}
};
int main(void)
{
	ThreadManager tm1(2);
	Task1 a;
	Task2 b;
	Task3 c;
	Task4 d;
	tm1.append(&a);
	tm1.append(&b);
	putchar(10);
	tm1.sync();
	tm1.append(&c);
	tm1.append(&d);
	return 0;
}
